<?php

declare( strict_types = 1 );

namespace App\Common\Model\Base\DataUnit;

use App\Common as com;
use App\Common\Model\Base as nameModelCommon;
use App\Common\Model\Base as nameBase;
use App\Common\Model\Base\DataUnit as nameDataUnit;
use App\Common\Model\Base\DataUnit\WhereDy\JoinWhere as nameJoinWhere;

class WhereDy extends nameDataUnit\Base {

	/**
	 * 是否存在动态where
	 * @return bool
	 * @throws
	 */
	private function isExistJoinWhere():bool {
		return $this->goPool( __FUNCTION__, function(){
			$classPath = static::class . "\JoinWhere";
			return class_exists( $classPath );
		} );
	}

	/**
	 * 动态where
	 * @return nameModelCommon\DataUnit\WhereDy\JoinWhere\Item
	 * @throws
	 */
	public function goJoinWhere():nameModelCommon\DataUnit\WhereDy\JoinWhere\Item {
		return $this->goPool( __FUNCTION__, function(){
			if( ! $this->isExistJoinWhere() ){
				throw com\Lib\Err\Exception::makeOfNew( '', com\Lib\Err\Code::$sysFileNotExist );
			}
			//
			$classPath = static::class . "\JoinWhere";
			return new $classPath();
		} );
	}

	/**
	 * @param int $id
	 * @return static
	 */
	public function id( int $id ):static {
		$this->getDataUnit()->goModelQuery()->where( $this->getDataUnit()->buildFieldPrefix( 'id' ), '=', $id );
		//
		return $this;
	}

	/**
	 * @param array $ids
	 * @return static
	 */
	public function ids( array $ids ):static {
		$this->getDataUnit()->goModelQuery()->whereIn( $this->getDataUnit()->buildFieldPrefix( 'id' ), $ids );
		//
		return $this;
	}

}
